using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using CheckedComboBox;
using ZoneFiveSoftware.Common.Data.Fitness;
using FilteredStatistics.Common;
using FilteredStatistics.Controller;
using FilteredStatistics.Data;

namespace FilteredStatistics.View
{
    class ZoneCategoriesDataGridViewCell : DataGridViewCell
    {
        public ZoneCategoriesDataGridViewCell()
            : base()
        {
            PlaceHolder.DisplayMember = "Name";
            PlaceHolder.ValueSeparator = PluginView.GetLocalizedString("OrText");
        }

        public override void InitializeEditingControl(int rowIndex,
                                                      object initialFormattedValue,
                                                      DataGridViewCellStyle dataGridViewCellStyle)
        {
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
                                          dataGridViewCellStyle);

            ZoneCategoriesDataGridViewEditingControl editingControl = DataGridView.EditingControl as ZoneCategoriesDataGridViewEditingControl;

            if (editingControl != null)
            {
                editingControl.ItemCheck += new ItemCheckEventHandler(OnCheckBoxCheckedChanged);
                editingControl.MaxDropDownItems = 7;
                editingControl.DisplayMember = "Name";
                editingControl.ValueSeparator = PluginView.GetLocalizedString("OrText");

                if (DataSource != null)
                {
                    editingControl.ListItems.Clear();

                    // Setup selected zones appropriately
                    foreach (INamedZone zone in DataSource)
                    {
                        NamedZoneCheckedComboBoxWrapper currenCBItem = new NamedZoneCheckedComboBoxWrapper(zone);

                        editingControl.ListItems.Add(currenCBItem);
                        editingControl.SetItemChecked(editingControl.ListItems.Count - 1, m_Value.Contains(currenCBItem.Zone));
                    }
                }
                else
                {
                    // Uncheck everything to prevent "ghost" display
                    for (int i = 0; i < editingControl.ListItems.Count; ++i)
                    {
                        editingControl.SetItemChecked(i, false);
                    }
                    editingControl.ListItems.Clear();

                    editingControl.DataSource = null;
                    editingControl.Enabled = false;
                    editingControl.HideDropDown();
                }
            }
        }

        private void OnCheckBoxCheckedChanged(object sender, ItemCheckEventArgs e)
        {
            NamedZoneCheckedComboBoxWrapper checkedItem = sender as NamedZoneCheckedComboBoxWrapper;

            if (checkedItem != null)
            {
                for (int i = 0; i < PlaceHolder.ListItems.Count; ++i)
                {
                    NamedZoneCheckedComboBoxWrapper item = PlaceHolder.ListItems[i] as NamedZoneCheckedComboBoxWrapper;

                    if (item.Zone == checkedItem.Zone)
                    {
                        PlaceHolder.SetItemChecked(i, true);
                        break;
                    }
                }
            }
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);

            if (graphics != null && clipBounds != null && cellBounds != null && formattedValue != null && advancedBorderStyle != null)
            {
                ZoneCategoriesDataGridViewEditingControl editingControl = DataGridView.EditingControl as ZoneCategoriesDataGridViewEditingControl;

                if (editingControl == null || editingControl.EditingControlRowIndex != RowIndex)
                {
                    bool isPlaceHolderEnabled = true;

                    if (m_PlaceHolderRender == null ||
                        m_PlaceHolderRender.Width != cellBounds.Width ||
                        m_PlaceHolderRender.Height != cellBounds.Height)
                    {
                        if (m_PlaceHolderRender != null)
                        {
                            m_PlaceHolderRender.Dispose();
                        }

                        PlaceHolder.Width = cellBounds.Width;
                        PlaceHolder.Height = cellBounds.Height;
                        m_PlaceHolderRender = new Bitmap(cellBounds.Width, cellBounds.Height);
                    }

                    if (RowIndex == -1)
                    {
                        isPlaceHolderEnabled = false;
                    }
                    else
                    {
                        DataGridViewComboBoxCell zoneTypeCell = DataGridView.Rows[RowIndex].Cells[ColumnIndex - 1] as DataGridViewComboBoxCell;
                        Debug.Assert(zoneTypeCell != null);

                        isPlaceHolderEnabled = (string)(zoneTypeCell.EditedFormattedValue) != String.Empty;
                    }

                    PlaceHolder.Enabled = isPlaceHolderEnabled;
                    PlaceHolder.DrawToBitmap(m_PlaceHolderRender, new Rectangle(0, 0, PlaceHolder.Width, PlaceHolder.Height));

                    // Render the text value
                    if (m_PlaceHolderText != String.Empty)
                    {
                        Graphics tempGraphics = Graphics.FromImage(m_PlaceHolderRender);
                        Brush textBrush = new SolidBrush(PlaceHolder.ForeColor);
                        StringFormat textFormat = new StringFormat(StringFormatFlags.NoWrap);

                        tempGraphics.DrawString(m_PlaceHolderText, PlaceHolder.Font, textBrush, new Rectangle(2, 4, PlaceHolder.Width - 16, PlaceHolder.Height - 4), textFormat);

                        tempGraphics.Dispose();
                        textBrush.Dispose();
                        textFormat.Dispose();
                    }

                    Utils.RenderBitmapToGraphics(m_PlaceHolderRender, graphics, cellBounds);
                }
            }
        }

        public override Type EditType
        {
            get { return typeof(ZoneCategoriesDataGridViewEditingControl); }
        }

        public override Type ValueType
        {
            get { return typeof(string); }
        }

        public CheckedListBox.ObjectCollection Items
        {
            get
            {
                return m_PlaceHolder.ListItems;
            }
        }

        public CheckedComboBoxControl PlaceHolder
        {
            get { return m_PlaceHolder; }
        }

        public IList<INamedZone> DataSource
        {
            get { return m_DataSource; }
            set
            {
                m_DataSource = value;

                PlaceHolder.ListItems.Clear();
                if (value != null)
                {
                    foreach (INamedZone zone in m_DataSource)
                    {
                        NamedZoneCheckedComboBoxWrapper currenCBItem = new NamedZoneCheckedComboBoxWrapper(zone);

                        PlaceHolder.ListItems.Add(currenCBItem);
                    }
                }
            }
        }

        public new List<INamedZone> Value
        {
            set
            {
                m_Value = value;

                if (m_Value != null)
                {
                    for (int i = 0; i < PlaceHolder.ListItems.Count; ++i)
                    {
                        NamedZoneCheckedComboBoxWrapper currentItem = PlaceHolder.ListItems[i] as NamedZoneCheckedComboBoxWrapper;

                        PlaceHolder.SetItemChecked(i, value.Contains(currentItem.Zone));
                    }

                    PlaceHolder.ValueSeparator = PluginView.GetLocalizedString("OrText");
                    m_PlaceHolderText = PlaceHolder.CheckedItemsStringValue;

                    for (int i = 0; i < PlaceHolder.ListItems.Count; ++i)
                    {
                        PlaceHolder.SetItemChecked(i, false);
                    }

                }
                else
                {
                    m_PlaceHolderText = "";
                }
            }
        }

        private CheckedComboBoxControl m_PlaceHolder = new CheckedComboBoxControl();
        private String m_PlaceHolderText;
        private Bitmap m_PlaceHolderRender;

        private IList<INamedZone> m_DataSource;
        private IList<INamedZone> m_Value;
    }
}
